/**
 * 
 */
package org.dftproject.lineagelinkage.utils;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;

/**
 * @author Steve
 * @invariant
 */
public class EasyFileUtils {

	/*
	 * Change the contents of text file in its entirety, overwriting any
	 * existing text. This style of implementation throws all exceptions to the
	 * caller. @param aFile is an existing file which can be written to. @throws
	 * IllegalArgumentException if param does not comply. @throws
	 * FileNotFoundException if the file does not exist. @throws IOException if
	 * problem encountered during write.
	 */
	static public void writeFile(File aFile, String aContents)
			throws FileNotFoundException, IOException {
		if (aFile == null)
		{
			throw new IllegalArgumentException("File should not be null.");
		}
		// declared here only to make visible to finally clause; generic
		// reference
		Writer output = null;
		try
		{
			// use buffering
			// FileWriter always assumes default encoding is OK!
			output = new BufferedWriter(new FileWriter(aFile));
			output.write(aContents);
		} finally
		{
			// flush and close both "output" and its underlying FileWriter
			if (output != null)
				output.close();
		}
	}
}
